<!DOCTYPE html>
<html lang="zh-cmn-Hans">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,viewport-fit=cover">
		<title>设置</title>
		<link rel="stylesheet" href="../../css/ui.min.css" />
		<link rel="stylesheet" href="../../css/iconfont.css">
		<link rel="stylesheet" href="../../css/mui.css" />
		<link rel="stylesheet" href="../../css/shop.css" />
	</head>
	<style type="text/css">
		.ui-ucenter-cells {
			margin-top: 10px;
		}

		.mui-table-view {
			width: 100%;
		}
		#head-img1{
			width: 60px;
			margin-left: -20px;
		}
		#update{
			margin-top:10px;
		}
	</style>
	<body ontouchstart>
		<header class="ui-header">
			<a href="javascript:history.go(-1)" class="mui-action-back ui-icon hd-left">
				<span class="iconfont icon-fanhui"></span>
			</a>
			<div class="ui-title">设置</div>
		</header>
		<div class="ui-content">
			<div class="ui-ucenter-cells">
				<li class="mui-table-view-cell">
					<a id="head" class="mui-navigate-right" style="line-height:57.55px;">头像
						<span class="mui-pull-right head">
							<img class="head-img mui-action-preview" id="head-img1" src="../../images/tx.jpg"/>
						</span>
					</a>
				</li>
			</div>
			<div class="ui-ucenter-cells">
				<div class="ui-cell ui-ucell" id="update">
					<div class="ui-cell__bd">
						<p>版本型号</p>
					</div>
					<div class="u-tips">V1.0</div>
				</div>
				<div id="sz-wtfk.html" class="ui-cell ui-cell_access ui-ucell wtfk">
					<div class="ui-cell__bd">
						<p>问题反馈</p>
					</div>
				</div>
			</div>
			<ul class="mui-table-view tuichu" id="../loging/loging.html">
				<li class="mui-table-view-cell" style="text-align: center;">
					<a>退出登录</a>
				</li>
			</ul>
		</div>
		<script src="../../js/mui.js"></script>
		<script>
// 			var old_back = mui.back;
// 			mui.back = function() {
// 				mui.currentWebview.close();
// 				old_back();
// 			}
			mui(".ui-content").on("tap", ".wtfk", function() {
				var url = this.getAttribute("id"); //getAttribute()获取属性函数
				mui.openWindow({
					url: url,
					id: url,
					styles: {
						top: '0px', //新页面顶部位置
						bottom: '0px', //新页面底部位置
						popGesture: 'close'
					},
					extras: {},
					waiting: {
						autoShow: false
					}
				})
			 });
			//检查更新
			document.getElementById("update").addEventListener('tap', function() {
				var server = "http://www.dcloud.io/check/update"; //获取升级描述文件服务器地址
				mui.getJSON(server, {
					"appid": plus.runtime.appid,
					"version": plus.runtime.version,
					"imei": plus.device.imei
				}, function(data) {
					if (data.status) {
						plus.ui.confirm(data.note, function(i) {
							if (0 == i) {
								plus.runtime.openURL(data.url);
							}
						}, data.title, ["立即更新", "取　　消"]);
					} else {
						mui.toast('已是最新版本~')
					}
				});
			});
			mui(".ui-content").on("tap", ".tuichu", function() {
				
				localStorage.removeItem("user_id");
				localStorage.removeItem("token");
				
				var url = this.getAttribute("id"); //getAttribute()获取属性函数
				mui.openWindow({
					url: url,
					id: url,
					styles: {
						top: '0px', //新页面顶部位置
						bottom: '0px', //新页面底部位置
						popGesture: 'close'
					},
					extras: {},
					waiting: {
						autoShow: false
					}
				})
			});
			//更换头像
		mui(".mui-table-view-cell").on("tap", "#head", function(e) {
			if(mui.os.plus){
				var a = [{
					title: "拍照"
				}, {
					title: "从手机相册选择"
				}];
				plus.nativeUI.actionSheet({
					title: "修改头像",
					cancel: "取消",
					buttons: a
				}, function(b) {
					switch (b.index) {
						case 0:
							break;
						case 1:
							getImage();
							break;
						case 2:
							galleryImg();
							break;
						default:
							break
					}
				})	
			}
			
		});

		function getImage() {
			var c = plus.camera.getCamera();
			c.captureImage(function(e) {
				plus.io.resolveLocalFileSystemURL(e, function(entry) {
					var s = entry.toLocalURL() + "?version=" + new Date().getTime();
					console.log(s);
					document.getElementById("head-img").src = s;
					document.getElementById("head-img1").src = s;
					//变更大图预览的src
					//目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现
					document.querySelector("#__mui-imageview__group .mui-slider-item img").src = s + "?version=" + new Date().getTime();;;
				}, function(e) {
					console.log("读取拍照文件错误：" + e.message);
				});
			}, function(s) {
				console.log("error" + s);
			}, {
				filename: "_doc/head.jpg"
			})
		}

		function galleryImg() {
			plus.gallery.pick(function(a) {
				plus.io.resolveLocalFileSystemURL(a, function(entry) {
					plus.io.resolveLocalFileSystemURL("_doc/", function(root) {
						root.getFile("head.jpg", {}, function(file) {
							//文件已存在
							file.remove(function() {
								console.log("file remove success");
								entry.copyTo(root, 'head.jpg', function(e) {
										var e = e.fullPath + "?version=" + new Date().getTime();
										document.getElementById("head-img").src = e;
										document.getElementById("head-img1").src = e;
										//变更大图预览的src
										//目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现
										document.querySelector("#__mui-imageview__group .mui-slider-item img").src = e + "?version=" + new Date().getTime();;
									},
									function(e) {
										console.log('copy image fail:' + e.message);
									});
							}, function() {
								console.log("delete image fail:" + e.message);
							});
						}, function() {
							//文件不存在
							entry.copyTo(root, 'head.jpg', function(e) {
									var path = e.fullPath + "?version=" + new Date().getTime();
									document.getElementById("head-img").src = path;
									document.getElementById("head-img1").src = path;
									//变更大图预览的src
									//目前仅有一张图片，暂时如此处理，后续需要通过标准组件实现
									document.querySelector("#__mui-imageview__group .mui-slider-item img").src = path;
								},
								function(e) {
									console.log('copy image fail:' + e.message);
								});
						});
					}, function(e) {
						console.log("get _www folder fail");
					})
				}, function(e) {
					console.log("读取拍照文件错误：" + e.message);
				});
			}, function(a) {}, {
				filter: "image"
			})
		};

		function defaultImg() {
			if(mui.os.plus){
				plus.io.resolveLocalFileSystemURL("_doc/head.jpg", function(entry) {
					var s = entry.fullPath + "?version=" + new Date().getTime();;
					document.getElementById("head-img").src = s;
					document.getElementById("head-img1").src = s;
				}, function(e) {
					document.getElementById("head-img").src = '../../images/tx.jpg';
					document.getElementById("head-img1").src = '../../images/tx.jpg';
				})
			}else{
				document.getElementById("head-img").src = '../../images/tx.jpg';
				document.getElementById("head-img1").src = '../../images/tx.jpg';
				
			}
			
		}
		document.getElementById("head-img1").addEventListener('tap', function(e) {
			e.stopPropagation();
		});
		

		function initImgPreview() {
			var imgs = document.querySelectorAll("img.mui-action-preview");
			imgs = mui.slice.call(imgs);
			if (imgs && imgs.length > 0) {
				var slider = document.createElement("div");
				slider.setAttribute("id", "__mui-imageview__");
				slider.classList.add("mui-slider");
				slider.classList.add("mui-fullscreen");
				slider.style.display = "none";
				slider.addEventListener("tap", function() {
					slider.style.display = "none";
				});
				slider.addEventListener("touchmove", function(event) {
					event.preventDefault();
				})
				var slider_group = document.createElement("div");
				slider_group.setAttribute("id", "__mui-imageview__group");
				slider_group.classList.add("mui-slider-group");
				imgs.forEach(function(value, index, array) {
					//给图片添加点击事件，触发预览显示；
					value.addEventListener('tap', function() {
						slider.style.display = "block";
						_slider.refresh();
						_slider.gotoItem(index, 0);
					})
					var item = document.createElement("div");
					item.classList.add("mui-slider-item");
					var a = document.createElement("a");
					var img = document.createElement("img");
					img.setAttribute("src", value.src);
					a.appendChild(img)
					item.appendChild(a);
					slider_group.appendChild(item);
				});
				slider.appendChild(slider_group);
				document.body.appendChild(slider);
				var _slider = mui(slider).slider();
			}
		}
		
		</script>
	</body>
</html>
